package org.game.module.fight.dungeom;

import java.util.Map;

import org.cfg.DungeonLevelCfg;
import org.cfg.DungeonLevelGroupCfg;
import org.constant.EventConditionKey;
import org.constant.EventType;
import org.error.GameErrorCode;
import org.game.constant.DungeonConstant;
import org.game.db.bean.DungeonLevel;
import org.game.db.bean.DungeonLevelGroup;
import org.game.db.bean.Formation;
import org.game.db.bean.Player;
import org.game.db.cache.DungeonLevelCache;
import org.game.manager.DungeonManager;
import org.game.manager.ItemManager;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventPlugin;
import org.game.protobuf.c2s.C2SDungeonMsg;
import org.utils.timeframe.TimeFrameChecker;

import com.google.common.collect.Maps;
import com.sojoys.artifact.tools.ToolError;

/**
 * @author : DengYing
 * @CreateDate : 2018年8月14日 下午7:50:43
 * @Description ：关卡相关检查
 */
public class DungeonCheck {
	
	/**
     * 检查前置关卡是否通关
     */
    public static void checkPreLevel(Player player, DungeonLevelCfg dungeonLevelCfg) {
        if (dungeonLevelCfg.getPreLevelId() == 0)
            return;
        DungeonLevel preDungeonLevel = DungeonLevelCache.me().getDungeonLevelCacheProxy().getByPlayerIdCid(player.getId(), dungeonLevelCfg.getPreLevelId());
        ToolError.isAndTrue(GameErrorCode.PRE_LEVEL_NOT_PASS, preDungeonLevel != null && !preDungeonLevel.isWin() || preDungeonLevel == null);
    }

    
    /**
     * 检查战斗次数
     */
    public static void checkFightCount(DungeonLevelCfg dungeonLevelCfg, DungeonLevel dungeonLevel) {
        if (dungeonLevel == null)
            return;
        ToolError.isAndTrue(GameErrorCode.FIGHT_COUNT_IS_UPPER_LIMIT,dungeonLevel.getFightCount() >= dungeonLevelCfg.getFightCount());
    }
    
    
    /**
     * 检查消耗
     */
    public static void checkCost(Player player, DungeonLevelCfg dungeonLevelCfg) {
        boolean enough = ItemManager.checkGoodsByCid(player, dungeonLevelCfg.getCost());
        ToolError.isAndTrue(GameErrorCode.GOODS_IS_NOT_ENOUGH, !enough);
    }
    
    /**
     * 检查玩家等级
     */
    public static void checkPlayerLevel(Player player, DungeonLevelCfg dungeonLevelCfg) {
        ToolError.isAndTrue(GameErrorCode.PLAYER_LEVEL_IS_NOT_ENOUGH, "玩家等级不足", player.getLevel() < dungeonLevelCfg.getPlayerLv());
    }

    
    /**
     * 检查关卡是否开启
     */
    public static void checkLevelGroupIsOpenTime(DungeonLevelGroupCfg dungeonLevelGroupCfg) {
        ToolError.isAndTrue(GameErrorCode.DUNGEON_LEVEL_IS_NOT_OPEN, "未到关卡开启时间", !TimeFrameChecker.check(dungeonLevelGroupCfg));
    }
    
    /**
     * 检查战斗次数
     */
    public static void checkLevelGroupFightCount(Player player, DungeonLevelGroupCfg dungeonLevelGroupCfg) {
        DungeonLevelGroup group = DungeonManager.getOrInitDungeonLevelGroup(player, dungeonLevelGroupCfg.getId());
        ToolError.isAndTrue(GameErrorCode.NOT_MORE_FIGHT_COUNT, "战斗次数已达上限", group.getDungeonLevelGroupProxy().checkFightCountIsLimit());
    }
    
    public static void fightEvent(Player player, DungeonLevel dungeonLevel, int oldStar, int nowStar,DungeonLevelCfg dungeonLevelCfg,C2SDungeonMsg.FightOverMsg msg) {
		// 触发事件
		Map<String, Object> in = Maps.newHashMap();
		// 关卡ID
		in.put(EventConditionKey.DUNGEON_CID, dungeonLevel.getCid());
		// 本次星数
		in.put(EventConditionKey.STAR, dungeonLevel.getStar());
		// 关卡难度
		in.put(EventConditionKey.DIFFICULTY, dungeonLevelCfg.getDifficulty());

		// 新增3星关卡通关
		if (oldStar!=nowStar && nowStar == DungeonConstant.CHAPTER_MAX_STAR) {
			in.put(EventConditionKey.FIRST_3_STAR, 1);
		}
		// 新增星数
		int addStar = nowStar - oldStar;
		if (addStar > 0) {
			in.put(EventConditionKey.ADD_STAR, addStar);
		}
		// 参与战斗英雄数
		Formation formation = player.getPlayerProxy().getCurrentFormation();
		in.put(EventConditionKey.FIGHT_HERO_COUNT, formation.getStance().size());
		// 战斗持续时间
		in.put(EventConditionKey.FIGHT_TIME, 0);
		if (msg != null) {
			// 本次最大连击数
			in.put(EventConditionKey.BATTER, msg.getBatter());
			// 拾取数量
			in.put(EventConditionKey.PICK_UP_COUNT, msg.getPickUpCount());
			// 拾取类型数量
			in.put(EventConditionKey.PICK_UP_TYPE_COUNT, msg.getPickUpTypeCount());
		}
		
		
		
		// 副本类型
		in.put(EventConditionKey.DUNGOEN_TYPE, dungeonLevelCfg.getDungeonType());

		GameEvent event = new GameEvent(player, EventType.PASS_DUP, in);
		GameEventPlugin.syncSubmit(event);
	}
}
